package com.example.myapplication.found.model;

import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @author: rongfeng
 * @date: 2022/8/9
 * @description 上涨前一天分析
 */
public class Fenxi8 extends BaseData {
    public static final int type = 17;

    public Fenxi8(StockBean stockBean, StockBean weekStockBean) {
        super(stockBean, weekStockBean);
    }

    public volatile static Vector<StockKlineBean> lastList = new Vector<>();
    static volatile List<String> codeList = new ArrayList<>();

    public static class Range {
        public int start;
        public int end;

        public Range(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    public static Vector<Range> rangeList = new Vector<>();
    public static int rangeIndex = 0;

    static {
        rangeList.add(new Range(0, 1));
        rangeList.add(new Range(1, 2));
        rangeList.add(new Range(2, 3));
        rangeList.add(new Range(3, 4));
        rangeList.add(new Range(4, 5));
        rangeList.add(new Range(5, 6));
        rangeList.add(new Range(6, 7));
        rangeList.add(new Range(7, 8));
        rangeList.add(new Range(8, 9));
        rangeList.add(new Range(9, 10));
        rangeList.add(new Range(11, 12));
        rangeList.add(new Range(12, 13));
        rangeList.add(new Range(13, 14));
        rangeList.add(new Range(14, 15));
        rangeList.add(new Range(15, 16));
        rangeList.add(new Range(16, 17));
        rangeList.add(new Range(17, 18));
        rangeList.add(new Range(18, 19));
        rangeList.add(new Range(19, 20));
    }

    @Override
    public void start() {
        if (!codeList.contains(fundCode)) {
            codeList.add(fundCode);
        } else {
            if (callback != null) {
                callback.fenxiCallback(1);
            }
            return;
        }
        Fenxi8.Range range = rangeList.get(rangeIndex);
        for (int i = 0; i < stockBean.klineList.size(); i++) {
            StockKlineBean bean = stockBean.klineList.get(i);
            if (bean.zhenfu > range.start && bean.zhenfu <= range.end) {
                if (i + 1 < stockBean.klineList.size()) {
                    StockKlineBean lastBean = stockBean.klineList.get(i + 1);
                    lastList.add(lastBean);
                }
            }
        }
        callback.fenxiCallback(1);
    }

    public static void analysis() {
        double chgAvg = 0;
        int zhang = 0;
        int die = 0;
        double avg1 = 0;
        for (int i = 0; i < lastList.size(); i++) {
            StockKlineBean stockKlineBean = lastList.get(i);
            chgAvg += stockKlineBean.chg;
            if (stockKlineBean.chg > 0) {
                zhang++;
            } else if (stockKlineBean.chg < 0) {
                die++;
            }
        }

        Fenxi8.Range range = rangeList.get(rangeIndex);
        System.out.println("范围  " + range.start + " " + range.end);
        System.out.println("涨次数 " + zhang + " " + ((float) zhang / (zhang + die)) * 100);
        System.out.println("跌次数 " + die + " " + ((float) die / (zhang + die)) * 100);
        System.out.println("涨跌幅平均 " + (chgAvg / lastList.size()));
        System.out.println();
        rangeIndex++;
        lastList.clear();
        codeList.clear();
    }
}
